package com.sino.operation.service.core;

import cn.hutool.core.util.ObjectUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.sino.operation.commons.commonEnum.ChargeInfoEnum;
import com.sino.operation.commons.commonEnum.TaskStateEnum;
import com.sino.operation.entity.BizAccountmanageInfo;
import com.sino.operation.entity.BizTranslist;
import com.sino.operation.peripheral.esc.request.CoreHead;
import com.sino.operation.peripheral.esc.request.core.EscAccountTest;
import com.sino.operation.peripheral.esc.request.core.body.*;
import com.sino.operation.peripheral.esc.response.EscResSysHead;
import com.sino.operation.peripheral.esc.response.EscRet;
import com.sino.operation.peripheral.esc.response.core.EscCoreDto;
import com.sino.operation.peripheral.esc.response.core.body.EscResBody2110;
import com.sino.operation.peripheral.esc.response.core.body.EscResBody4010;
import com.sino.operation.peripheral.esc.response.core.body.EscResBody4020;
import com.sino.operation.peripheral.esc.response.core.body.EscResBody4022;
import com.sino.operation.service.business.ObtainEscCoreParamService;
import com.sino.operation.service.core.entity.PostCoreRecive;
import com.sino.operation.util.*;
import com.sino.operation.vo.current.InfoInput;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @ClassName Core4010Service
 * @Description 用于单位活期开户建立客户信息的接口业务类
 * @Author 北陈
 * @Date 2021/11/8 16:48
 * @Version 1.0
 */
@Service
@Slf4j
public class CoreService {

    @Autowired
    private ObtainEscCoreParamService obtainEscCoreParamService;

    @Autowired
    private UrlUtil urlUtil;

    @Autowired
    private RestTemplateUtils restTemplateUtils;

    /**
     * @Description: ${对公活期建立客户信息}
     * param [bizTranslist, bizAccountmanageInfo, infoInputs, map]
     * return com.sino.operation.service.core.entity.PostCoreRecive
     * author 北陈
     * date 2021/11/8 18:21
    */
    public PostCoreRecive core4010(BizTranslist bizTranslist, BizAccountmanageInfo bizAccountmanageInfo,List<InfoInput> infoInputs,Map map){
        PostCoreRecive postCoreRecive = new PostCoreRecive();
        postCoreRecive.setState(false);
        String journalNo = bizTranslist.getJournalNo();

        CoreHead coreHead = new CoreHead();
        coreHead.setReqResFlg("req");

        Map<String, Object> mapPost4010 = EscBodyMapTestUtil.getMap(EscBodyCore4010.class);
        //利用对应的字段进行赋值
        for (Map.Entry<String, Object> key : mapPost4010.entrySet()) {
            mapPost4010.put(key.getKey(), map.get(key.getKey()));
        }
        //加上核心字段请求
        mapPost4010.put("CORE_HEAD", coreHead);
        AccountGetArrayUtil.addCoreNewAccountByPublic(infoInputs, mapPost4010);
        EscAccountTest escAccountTest = this.obtainEscCoreParamService.getEscCoreParam(journalNo, "4010");
        escAccountTest.setMap(mapPost4010);
        //获取核心单位活期建立客户信息接口地址
        String core4010Url = this.urlUtil.getCoreUrl("4010");
        try {
            String value = new ObjectMapper().writeValueAsString(escAccountTest);
            log.info("发往核心4010单位活期建立客户信息的报文", value);
            ResponseEntity<EscCoreDto<EscResBody4010>> post = restTemplateUtils
                    .exchange(core4010Url, HttpMethod.POST,
                            new HttpEntity<>(escAccountTest),
                            new ParameterizedTypeReference<EscCoreDto<EscResBody4010>>() {
                            });
            EscCoreDto<EscResBody4010> escCoreDto4010 = post.getBody();
            EscResSysHead escResSysHead = escCoreDto4010.getEscResSysHead();
            String retStatus = escResSysHead.getRetStatus();
            if (ChargeInfoEnum.CORE_BACK_ERROR.getCode().equals(retStatus)) {
                //建立客户信息失败
                List<EscRet> rets = escResSysHead.getRets();
                StringBuilder codeBuilder = new StringBuilder();
                StringBuilder desBuilder = new StringBuilder();
                rets.forEach(escRet -> {
                    if (ObjectUtil.isNotNull(escRet)) {
                        codeBuilder.append(escRet.getRetCode());
                        desBuilder.append(escRet.getRetMsg());
                    }
                });
                bizTranslist.setHxResponseDesc(desBuilder.toString() == null ? "" : desBuilder.toString());
                bizTranslist.setHxResponseCode(codeBuilder.toString() == null ? "" : codeBuilder.toString());
                bizTranslist.setTransState(TaskStateEnum.TRANS_ERROR.getCode());
                postCoreRecive.setState(false);
                return postCoreRecive;
            }
            //建立客户信息成功
            String clientNo = "";
            String clientNmCn = "";
            clientNo = escCoreDto4010.getEscResBody().getCustNum();//客户号
            clientNmCn = escCoreDto4010.getEscResBody().getCustChinsNm();//账户号
            bizTranslist.setClientNo(clientNo);
            bizAccountmanageInfo.setCunm(clientNmCn);
            postCoreRecive.setState(true);
            postCoreRecive.setMap(mapPost4010);
            postCoreRecive.setEscCoreDto(escCoreDto4010);
            return postCoreRecive;
        } catch (Exception e) {
            log.error(TimeUtil.getStandardTime() + "发往核心建立单位活期客户信息出现错误", e.getMessage());
            bizTranslist.setHxResponseDesc("建立客户信息请求核心出现错误");
            bizTranslist.setTransState(TaskStateEnum.TRANS_ERROR.getCode());
            postCoreRecive.setState(false);
            return postCoreRecive;
        }
    }


    /**
     * @Description: ${同业活期建立客户信息}
     * param [bizTranslist, bizAccountmanageInfo, infoInputs, map]
     * return com.sino.operation.service.core.entity.PostCoreRecive
     * author 北陈
     * date 2021/11/8 18:21
    */
    public PostCoreRecive core4020(BizTranslist bizTranslist, BizAccountmanageInfo bizAccountmanageInfo,List<InfoInput> infoInputs,Map map){
        PostCoreRecive postCoreRecive = new PostCoreRecive();
        postCoreRecive.setState(false);

        String journalNo = bizTranslist.getJournalNo();

        CoreHead coreHead = new CoreHead();
        coreHead.setReqResFlg("req");

        //同业活期建立客户信息
        Map<String, Object> mapPost4020 = EscBodyMapTestUtil.getMap(EscBodyCore4020.class);
        //利用对应的字段进行赋值
        for (Map.Entry<String, Object> key : mapPost4020.entrySet()) {
            mapPost4020.put(key.getKey(), map.get(key.getKey()));
        }
        mapPost4020.put("CORE_HEAD", coreHead);
        AccountGetArrayUtil.addCoreNewAccountByPeers(infoInputs, mapPost4020);
        EscAccountTest escAccountTest = this.obtainEscCoreParamService.getEscCoreParam(journalNo, "4020");
        escAccountTest.setMap(mapPost4020);
        //获取发往核心的同业活期建立客户信息的url
        String coreUrl4020 = this.urlUtil.getCoreUrl("4020");
        try {
            String value = new ObjectMapper().writeValueAsString(escAccountTest);
            log.info("发往核心4010单位活期建立客户信息的报文", value);
            ResponseEntity<EscCoreDto<EscResBody4020>> post = restTemplateUtils
                    .exchange(coreUrl4020, HttpMethod.POST,
                            new HttpEntity<>(escAccountTest),
                            new ParameterizedTypeReference<EscCoreDto<EscResBody4020>>() {
                            });
            EscCoreDto<EscResBody4020> escCoreDto4020 = post.getBody();
            EscResSysHead escResSysHead = escCoreDto4020.getEscResSysHead();
            String retStatus = escResSysHead.getRetStatus();
            if (ChargeInfoEnum.CORE_BACK_ERROR.getCode().equals(retStatus)) {
                //建立客户信息失败
                List<EscRet> rets = escResSysHead.getRets();
                StringBuilder codeBuilder = new StringBuilder();
                StringBuilder desBuilder = new StringBuilder();
                rets.forEach(escRet -> {
                    if (ObjectUtil.isNotNull(escRet)) {
                        codeBuilder.append(escRet.getRetCode());
                        desBuilder.append(escRet.getRetMsg());
                    }
                });
                bizTranslist.setHxResponseDesc(desBuilder.toString() == null ? "" : desBuilder.toString());
                bizTranslist.setHxResponseCode(codeBuilder.toString() == null ? "" : codeBuilder.toString());
                bizTranslist.setTransState(TaskStateEnum.TRANS_ERROR.getCode());
                postCoreRecive.setState(false);
                return postCoreRecive;
            }
            //建立客户信息成功
            String clientNo = "";
            String clientNmCn = "";
            clientNo = escCoreDto4020.getEscResBody().getCustNum();//客户号
            clientNmCn = escCoreDto4020.getEscResBody().getCustChinsNm();//客户中文名
            bizTranslist.setClientNo(clientNo);
            bizAccountmanageInfo.setCunm(clientNmCn);
            postCoreRecive.setState(true);
            postCoreRecive.setMap(mapPost4020);
            postCoreRecive.setEscCoreDto(escCoreDto4020);
            return postCoreRecive;

        } catch (Exception e) {
            log.error(TimeUtil.getStandardTime()+"流水号："+journalNo + "发往核心建立同业活期客户信息出现错误", e.getMessage());
            bizTranslist.setHxResponseDesc("建立客户信息请求核心出现错误");
            bizTranslist.setTransState(TaskStateEnum.TRANS_ERROR.getCode());
            postCoreRecive.setState(false);
            return postCoreRecive;
        }

    }

    /**
     *
     * @description {同业活期变更客户信息}
     * @param bizTranslist
     * @param bizAccountmanageInfo
     * @param infoInputs
     * @return com.sino.operation.service.core.entity.PostCoreRecive
     * @author 北陈
     * @date 2021/11/11 0011 0:38
     **/
    public PostCoreRecive core4022(BizTranslist bizTranslist,BizAccountmanageInfo bizAccountmanageInfo,List<InfoInput> infoInputs,Map map){
        PostCoreRecive postCoreRecive = new PostCoreRecive();
        postCoreRecive.setState(false);

        String journalNo = bizTranslist.getJournalNo();

        CoreHead coreHead = new CoreHead();
        coreHead.setReqResFlg("req");

        //同业活期建立客户信息
        Map<String, Object> mapPost4022 = EscBodyMapTestUtil.getMap(EscBodyCore4022.class);
        //利用对应的字段进行赋值
        for (Map.Entry<String, Object> key : mapPost4022.entrySet()) {
            mapPost4022.put(key.getKey(), map.get(key.getKey()));
        }
        mapPost4022.put("CORE_HEAD", coreHead);
        AccountGetArrayUtil.changeCoreNewAccountByPeers(infoInputs, mapPost4022);
        EscAccountTest escAccountTest = this.obtainEscCoreParamService.getEscCoreParam(journalNo, "4022");
        escAccountTest.setMap(mapPost4022);
        //获取发往核心的同业活期建立客户信息的url
        String coreUrl4022 = this.urlUtil.getCoreUrl("4022");
        try {
            String value = new ObjectMapper().writeValueAsString(escAccountTest);
            log.info("发往核心4022同业活期变更客户信息的报文", value);
            ResponseEntity<EscCoreDto<EscResBody4022>> post = restTemplateUtils
                    .exchange(coreUrl4022, HttpMethod.POST,
                            new HttpEntity<>(escAccountTest),
                            new ParameterizedTypeReference<EscCoreDto<EscResBody4022>>() {
                            });
            EscCoreDto<EscResBody4022> escCoreDto4022 = post.getBody();
            EscResSysHead escResSysHead = escCoreDto4022.getEscResSysHead();
            String retStatus = escResSysHead.getRetStatus();
            if (ChargeInfoEnum.CORE_BACK_ERROR.getCode().equals(retStatus)) {
                //变更客户信息失败
                List<EscRet> rets = escResSysHead.getRets();
                StringBuilder codeBuilder = new StringBuilder();
                StringBuilder desBuilder = new StringBuilder();
                rets.forEach(escRet -> {
                    if (ObjectUtil.isNotNull(escRet)) {
                        codeBuilder.append(escRet.getRetCode());
                        desBuilder.append(escRet.getRetMsg());
                    }
                });
                bizTranslist.setHxResponseDesc(desBuilder.toString() == null ? "" : desBuilder.toString());
                bizTranslist.setHxResponseCode(codeBuilder.toString() == null ? "" : codeBuilder.toString());
                bizTranslist.setTransState(TaskStateEnum.TRANS_ERROR.getCode());
                postCoreRecive.setState(false);
                return postCoreRecive;
            }
            //建立客户信息成功
            String clientNo = "";
            String clientNmCn = "";
            clientNo = escCoreDto4022.getEscResBody().getCustNum();//客户号
            clientNmCn = escCoreDto4022.getEscResBody().getCustChinsNm();//客户中文名
            bizTranslist.setClientNo(clientNo);
            bizAccountmanageInfo.setCunm(clientNmCn);
            postCoreRecive.setState(true);
            postCoreRecive.setMap(mapPost4022);
            postCoreRecive.setEscCoreDto(escCoreDto4022);
            return postCoreRecive;

        } catch (Exception e) {
            log.error(TimeUtil.getStandardTime()+"流水号："+journalNo + "发往核心变更同业活期客户信息出现错误", e.getMessage());
            bizTranslist.setHxResponseDesc("同业活期变更客户信息请求核心出现错误");
            bizTranslist.setTransState(TaskStateEnum.TRANS_ERROR.getCode());
            postCoreRecive.setState(false);
            return postCoreRecive;
        }
    }
    /**
     * @Description: ${对公活期开户}
     * param [bizTranslist, bizAccountmanageInfo, infoInputs, map]
     * return com.sino.operation.service.core.entity.PostCoreRecive
     * author 北陈
     * date 2021/11/8 18:21
    */
    public PostCoreRecive core2110(BizTranslist bizTranslist, BizAccountmanageInfo bizAccountmanageInfo,List<InfoInput> infoInputs,Map map){

        PostCoreRecive postCoreRecive = new PostCoreRecive();
        postCoreRecive.setState(false);

        String journalNo = bizTranslist.getJournalNo();

        CoreHead coreHead = new CoreHead();
        coreHead.setReqResFlg("req");

        Map<String, Object> mapPost2110 = EscBodyMapTestUtil.getMap(EscBodyCore2110.class);
        //利用对应的字段进行赋值
        for (Map.Entry<String, Object> key : mapPost2110.entrySet()) {
            mapPost2110.put(key.getKey(), map.get(key.getKey()));
        }
        //加上核心字段请求
        mapPost2110.put("CORE_HEAD", coreHead);
        AccountGetArrayUtil.addCoreByOpenAccount(infoInputs, mapPost2110);
        EscAccountTest escAccountTest2110 = this.obtainEscCoreParamService.getEscCoreParam(journalNo, "2110");
        //从开户中取出客户号和客户中文名字
        String clientNo = bizTranslist.getClientNo();
        mapPost2110.put("cust_num", clientNo);
        //mapPost2110.put("cust_acct_num", clientNo);
        escAccountTest2110.setMap(mapPost2110);

        try {
            String value2110 = new ObjectMapper().writeValueAsString(escAccountTest2110);
            log.info("发往核心2110单位活期开户的报文", value2110);
            String core2110Url = this.urlUtil.getCoreUrl("2110");
            ResponseEntity<EscCoreDto<EscResBody2110>> post2110 = restTemplateUtils
                    .exchange(core2110Url, HttpMethod.POST,
                            new HttpEntity<>(escAccountTest2110),
                            new ParameterizedTypeReference<EscCoreDto<EscResBody2110>>() {
                            });
            EscCoreDto<EscResBody2110> escCoreDto2110 = post2110.getBody();
            EscResSysHead escResSysHead = escCoreDto2110.getEscResSysHead();
            String retStatus = escResSysHead.getRetStatus();
            if (ChargeInfoEnum.CORE_BACK_ERROR.getCode().equals(retStatus)) {
                //开户失败
                List<EscRet> rets = escResSysHead.getRets();
                StringBuilder codeBuilder = new StringBuilder();
                StringBuilder desBuilder = new StringBuilder();
                rets.forEach(escRet -> {
                    if (ObjectUtil.isNotNull(escRet)) {
                        codeBuilder.append(escRet.getRetCode());
                        desBuilder.append(escRet.getRetMsg());
                    }
                });
                bizTranslist.setHxResponseDesc(desBuilder.toString() == null ? "" : desBuilder.toString());
                bizTranslist.setHxResponseCode(codeBuilder.toString() == null ? "" : codeBuilder.toString());
                bizTranslist.setTransState(TaskStateEnum.TRANS_ERROR.getCode());
                postCoreRecive.setState(false);
                return postCoreRecive;
            }
            //开户成功
            bizTranslist.setTransState(TaskStateEnum.TRANS_SUCCESS.getCode());
            bizAccountmanageInfo.setAccountNo(escCoreDto2110.getEscResBody().getCustAcctNum());
            postCoreRecive.setState(true);
            postCoreRecive.setMap(mapPost2110);
            postCoreRecive.setEscCoreDto(escCoreDto2110);
            return postCoreRecive;
        } catch (Exception e) {
            log.error(TimeUtil.getStandardTime() + "发往核心2110开户出现错误", e.getMessage());
            bizTranslist.setHxResponseDesc("活期开户核心出现错误");
            bizTranslist.setTransState(TaskStateEnum.TRANS_ERROR.getCode());
            postCoreRecive.setState(false);
            return postCoreRecive;
        }
    }

    /**
     *
     * @description {短信通}
     * @param bizTranslist
     * @param bizAccountmanageInfo
     * @param infoInputs
     * @param map
     * @return void
     * @author 北陈
     * @date 2021/11/18 0018 22:09
     **/
    public void core4511(BizTranslist bizTranslist, BizAccountmanageInfo bizAccountmanageInfo,List<InfoInput> infoInputs,Map map){
        String journalNo = bizTranslist.getJournalNo();

        CoreHead coreHead = new CoreHead();
        coreHead.setReqResFlg("req");

        Map<String, Object> mapPost4511 = EscBodyMapTestUtil.getMap(EscBodyCore4511.class);
        //利用对应的字段进行赋值
        for (Map.Entry<String, Object> key : mapPost4511.entrySet()) {
            mapPost4511.put(key.getKey(), map.get(key.getKey()));
        }
        //加上核心字段请求
        mapPost4511.put("CORE_HEAD", coreHead);
        AccountGetArrayUtil.addCoreByOpenAccount(infoInputs, mapPost4511);
        EscAccountTest escAccountTest2110 = this.obtainEscCoreParamService.getEscCoreParam(journalNo, "2110");
        //从开户中取出客户号和客户中文名字
        String clientNo = bizTranslist.getClientNo();
        mapPost4511.put("cust_num", clientNo);
        mapPost4511.put("cust_acct_num",bizAccountmanageInfo.getAccountNo());
        escAccountTest2110.setMap(mapPost4511);
        try{

        }catch(Exception e){
            
        }
    }
}
